module("MapScene", package.seeall)

function create()
	local scene = cc.Scene:create()

	init(scene)

	return scene
end

function init(scene)
    local screenSize = cc.Director:getInstance():getWinSize()

	--加载UI层
	local uiLayer = cc.Layer:create()
	local uiMap = ccs.GUIReader:getInstance():widgetFromJsonFile("res/UI/MapUI/MapUI.json")
	local uiStageDetail = ccs.GUIReader:getInstance():widgetFromJsonFile("res/UI/MapUI/StageDetail.json")

	uiMap:setSize(cc.size(screenSize.width, screenSize.height))
	uiStageDetail:setSize(cc.size(screenSize.width, screenSize.height))

	local buttonStage = uiMap:getChildByName("Image_Bg"):getChildByName("Button_Stage1")
	local buttonBack1 = uiMap:getChildByName("Image_Bg"):getChildByName("Button_Back")

	buttonStage:addTouchEventListener(
		function( sender, eventType )
			if (eventType == ccui.TouchEventType.ended) then
				uiLayer:addChild(uiStageDetail, 1)
			end
		end)
	buttonBack1:addTouchEventListener(
		function( sender, eventType )
			if (eventType == ccui.TouchEventType.ended) then
				SceneManager.switchToScene(HomeScene.create())
			end
		end)

	local buttonFight = uiStageDetail:getChildByName("Image_Bg"):getChildByName("Button_Fight")
	local buttonBack = uiStageDetail:getChildByName("Image_Bg"):getChildByName("Button_Back")

	buttonFight:addTouchEventListener(
		function( sender, eventType )
			if (eventType == ccui.TouchEventType.ended) then
				SceneManager.switchToScene(BattleScene.create())
			end
		end)
	buttonBack:addTouchEventListener(
		function( sender, eventType )
			if (eventType == ccui.TouchEventType.ended) then
				uiStageDetail:removeFromParent()
			end
		end)
	
	uiLayer:addChild(uiMap)
	uiStageDetail:retain()

	scene:addChild(uiLayer)
end

